<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    //当前用户的文章数
    public function  posts() {
        return $this->hasMany('App\Post','user_id','id');
    }
    //我的粉丝
    public function  fans() {
        return $this->hasMany('App\Fan','star_id','id');
    }
    /*
   * 我的关注
   */
    public function stars()
    {
        return $this->hasMany('App\Fan', 'fan_id', 'id');
    }

    /**
     * @param $uid
     * @return false|\Illuminate\Database\Eloquent\Model
     * 关注某人
     */
    public function  doFan($uid){
        $fan = new Fan();
        $fan->star_id = $uid;
        return $this->stars()->save($fan);
    }

    /**
     * @param $uid
     * @return mixed
     * 取消关注
     */
    public function  doUnFan($uid){
        $fan = new Fan();
        $fan->star_id = $uid;
        return $this->stars()->delete($fan);
    }

    /**
     * @param $uid
     * @return int
     * 当前用户是否被$uid关注
     */
    public function hasFan($uid)
    {
        return $this->fans()->where('fan_id', $uid)->count();
    }



}
